﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Permissions;

namespace Engine.Maps.Google
{

    [PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
    [System.Runtime.InteropServices.ComVisibleAttribute(true)]
    public class MapCommands
    {
        HtmlDocument doc;


       
        POI.PoiList tmpList = new POI.PoiList();
        bool docReady = false;

        public MapCommands()
        {            
        }


        public void SetDocument(HtmlDocument doc)
        {
            this.doc = doc;
                        
        }


        #region Called from javascript

        /// <summary>
        /// Indicated about all scripts are loaded and ready to be used.
        /// </summary>
        public void AllDocumentElementsReady()
        {            
            docReady = true;
            AddPOIs(tmpList);
            tmpList.Clear();
        }


        #endregion


        /// <summary>
        /// 
        /// </summary>
        /// <param name="p"></param>
        public void AddPOI(POI.BasePoi p)
        {
            POI.PoiList l = new POI.PoiList();
            l.Add(p);
            AddPOIs(l);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="list"></param>
        public void AddPOIs(POI.PoiList list)
        {            
            System.Web.Script.Serialization.JavaScriptSerializer json = new System.Web.Script.Serialization.JavaScriptSerializer();
            if (!docReady)
            {
                tmpList.AddRange(list.ToArray());
            }
            else
            {
                string str = json.Serialize(list.ConvertTo<POI.CameraPoi>().ToArray());
                object obj = doc.InvokeScript("loadPois", new string[] { str });  
            }
            
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="zoom"></param>
        public void ZoomOnPoint(float x, float y, int zoom)
        {
            if (!docReady) return;
            doc.InvokeScript ("setCenter",new object[] { x,y,zoom});
        }

    }
}
